﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class AccessorySelectByInstrument : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
            
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindGrid(true);
        }

        private void BindGrid(bool IsNeedBind)
        {
            StringBuilder sbWhereClause = new StringBuilder(" and A.InstrumentID in (select InstrumentID from Cms_Wsm_TaskRegister where RegisterID = '" + Request.QueryString["RegisterID"] + "')");
  
            if (!string.IsNullOrEmpty(txtAccessoryCode.Text.Trim()))
            {
                sbWhereClause.Append(" and B.CompanyInstrumentCode like '%").Append(txtAccessoryCode.Text.Trim()).Append("%'");
            }

            if (!string.IsNullOrEmpty(txtAccessoryName.Text.Trim()))
            {
                sbWhereClause.Append(" and B.AccessoryName like '%").Append(txtAccessoryName.Text.Trim()).Append("%'");
            }
            WsmInstrumentRecordBll bll = new WsmInstrumentRecordBll();

            gridAccessoryList.DataSource = bll.GetAccessoryEntities(sbWhereClause.ToString());

            if(IsNeedBind)
            gridAccessoryList.DataBind();
        } 

        protected void gridAccessoryList_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            BindGrid(false);
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "select")
            {
                string AccessoryID = gridAccessoryList.MasterTableView.DataKeyValues[e.Item.ItemIndex]["AccessoryID"].ToString();

                WsmAccessoryInfoEntity entity = null;
                StockBll bll = new StockBll();

                entity = bll.Get<WsmAccessoryInfoEntity>(new Guid(AccessoryID));
                string Script = "alert('请选择配件');";
                if (entity != null)
                {
                    decimal PersonalQuantity = 0;

                    PersonalQuantity = bll.GetPersonalStockQuantity(new Guid(AccessoryID), new Guid(Request.QueryString["MaintainID"]));

                      Script = @"var arg = new Array();
arg[0] = '{0}';
arg[1] = '{1}';
arg[2] = '{2}'; 
arg[3] = {3};
arg[4] = '{4}';
SaveAndCloseWindow1(arg);
";

                      Script = string.Format(Script, AccessoryID, entity.CompanyInstrumentCode, entity.AccessoryName, PersonalQuantity,entity.Vendor);

                }
                
                ClientScript.RegisterStartupScript(this.GetType(), "select", Script, true);
            }
        }
    }
}
